/**
 * @Company: 小明
 * @Author: 40313
 * @DATE: 2020/12/19 星期六 下午 9:46
 */
package com.situ.oa.user.controller;

import com.situ.oa.user.pojo.User;
import com.situ.oa.user.service.UserService;
import com.situ.oa.util.ServletUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * ClassName: Controller
 *
 * @Author: 40313
 * @Description:(类描述)
 */
@RestController
@RequestMapping("user")
public class UserController {
    private UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * @param modelAndView
     * @return
     * @Title: goIndex
     * @Description:(进模块管理首页)
     */
    @GetMapping
    public ModelAndView goIndex(ModelAndView modelAndView) {
        modelAndView.setViewName("user/user_index");
        return modelAndView;
    }

    /**
     * @param user         根据查询的用户信息
     * @param modelAndView
     * @return 返回查询的结果集合
     * @Title: doSearch
     * @Description:(多条件查询)
     */
    @PostMapping("/search")
    public ModelAndView doSearch(User user, ModelAndView modelAndView) {
        List<User> userList = userService.findSearchUser(user);
        modelAndView.addObject("userList", userList);
        modelAndView.setViewName("user/user_list");
        return modelAndView;
    }

    /**
     * @param rowId 要删除的主键
     * @return 受影响的集合
     * @Title: doDelete
     * @Description:(根据主键删除一个实例)
     */
    @DeleteMapping("/{rowId}")
    public Integer doDelete(@PathVariable Long rowId) {
        userService.deleteUser(rowId);
        return 1;
    }

    /**
     * @param user
     * @param session
     * @return 受影响的行数
     * @Title: doUpdate
     * @Description:(执行修改到数据库)
     */
    @PostMapping("/doupdate")
    public Integer doUpdate(User user, HttpSession session) {
        String updateBy = ServletUtil.getUserCodeBySession(session);
        userService.updateUser(user, updateBy);
        return 1;
    }

    /**
     * @param rowId        先根据主键查询一个实例，进入修改表单
     * @param modelAndView
     * @return 进入修改表单
     * @Title: goUpdate
     * @Description:(进修改表单)
     */
    @PostMapping("/{rowId}")
    public ModelAndView goUpdate(@PathVariable Long rowId, ModelAndView modelAndView) {
        User user = userService.findOneUser(rowId);
        System.out.println(user);
        modelAndView.addObject("user", user);
        modelAndView.setViewName("user/user_update");
        return modelAndView;
    }

    /**
     * @param modelAndView
     * @return
     * @Title: getAllUserList
     * @Description:(查询所有的用户信息)
     */
    @GetMapping("/list")
    public ModelAndView getAllUserList(ModelAndView modelAndView) {
        List<User> userList = userService.findAllUser();
        modelAndView.addObject("userList", userList);
        modelAndView.setViewName("user/user_list");
        return modelAndView;
    }

    /**
     * @param user
     * @return
     * @Title: doAddUser
     * @Description:(新增用户)
     */
    @PostMapping("/doadd")
    public Integer doAddUser(User user, HttpSession session) {
        System.out.println(user);
        //得到当前登录人的账号
        String createBy = ServletUtil.getUserCodeBySession(session);
        return userService.doAddUser(user, createBy);
    }


    /**
     * @param userFile
     * @param request
     * @return
     * @Title: doUpload
     * @Description:(上传用户图像)
     */
    @PostMapping("/upload")
    public String doUpload(MultipartFile userFile, HttpServletRequest request) {
        String realPath = request.getServletContext().getRealPath("/");
        return userService.doUploadAvatar(userFile, realPath);
    }

    /**
     * @param modelAndView
     * @return
     * @Title: goAddForm
     * @Description:(进新增表单)
     */
    @GetMapping("/form")
    public ModelAndView goAddForm(ModelAndView modelAndView) {
        modelAndView.setViewName("user/user_add");
        return modelAndView;
    }
}
